package com.treeroot.treenet.common.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Create By TreeRoot
 * Create Time: 2023/12/18 03:37
 * Class Name: StringUtils
 * Description:
 * 字符串工具类
 *
 * @author TreeRoot
 */
public class StringUtils {
    /**
     * 将字符串按照指定的分隔符拆分成字符串数组：split("a,b,c", ",") = ["a", "b", "c"]
     *
     * @param str       str
     * @param delimiter 分隔符
     * @return 字符串[]
     * 2023/11/07
     */

    public static String[] split(String str, String delimiter) {

        List<String> result = new ArrayList<>();
        int startIndex = 0;
        int endIndex;

        while ((endIndex = str.indexOf(delimiter, startIndex)) != -1) {
            result.add(str.substring(startIndex, endIndex));
            startIndex = endIndex + delimiter.length();
        }

        if (startIndex < str.length()) {
            result.add(str.substring(startIndex));
        }

        return result.toArray(new String[0]);
    }
}
